<html>
<head><meta charset="utf-8"><title>Issue clippy#6715 (clippy#6730) · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html">Issue clippy#6715 (clippy#6730)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226163228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226163228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226163228">(Feb 12 2021 at 16:52)</a>:</h4>
<p>It seems I did try a little too hard in my initial work to be smart especially for the "Additional Comments" section on that, but I'm wondering how smart I need to be (and how smart is too far)</p>
<p>I can totally put up a draft/WIP PR really quick if that would be helpful as well, but I think my current questions are:</p>
<ul>
<li>Am I doing this the right way at all -- which actually having the draft PR would be useful for</li>
<li>camsteffen mentioned on the issue: « Instead there should be a list of known "to owned" functions that are equivalent to <code>clone</code>. »  What is the standard way of having such a list? Is there an existing lint that has such a list I can look at?</li>
<li>Is it reasonable to assume that <code>to_owned</code> is always safe to turn into <code>clone</code> -- at least as long as <code>to_owned</code> returns the same type?</li>
</ul>
<p>It would probably be much easier to just make this only work on <code>Vec</code>, but adding some smarts would make me feel better instead of completely ignoring the "Additional Comments" -- and I like a challenge anyway.</p>



<a name="226164147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226164147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226164147">(Feb 12 2021 at 16:58)</a>:</h4>
<p>I would just create a list for those types and the methods of those types that are equal to <code>clone</code>.  </p>
<p>Another way this could maybe be done is to determine if the type behind the <code>to_owned</code> method call has to be derefed to another type, which then returns the same type before the deref. Not sure how hard that is though.</p>



<a name="226164949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226164949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226164949">(Feb 12 2021 at 17:03)</a>:</h4>
<p>In <code>vec.to_owned()</code>, you can get <code>expr_ty</code> for <code>vec</code> and <code>vec.to_owned()</code> (the method call and the first arg of the method call) and then check <code>TyS::same_type</code> on those types.</p>



<a name="226165591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226165591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226165591">(Feb 12 2021 at 17:08)</a>:</h4>
<p>See the big <code>match</code> with different method names in <code>methods.rs</code>. Instead of having a list, you can just add to that.</p>



<a name="226165718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226165718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226165718">(Feb 12 2021 at 17:09)</a>:</h4>
<blockquote>
<p>Is it reasonable to assume that to_owned is always safe to turn into clone -- at least as long as to_owned returns the same type?</p>
</blockquote>
<p>Yes.</p>



<a name="226166807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226166807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226166807">(Feb 12 2021 at 17:16)</a>:</h4>
<p>If this fires on arbitrary custom types, especially if it's not looking for the string <code>to_owned</code>, it will have a lot of false positives for methods that have the same type as clone but are semantically different, e.g. <code>new_subtree(self: &amp;Tree) -&gt; Tree</code>. A whitelist sounds a lot more reliable</p>



<a name="226167072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226167072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226167072">(Feb 12 2021 at 17:18)</a>:</h4>
<p>Right, after matching the method name, you should use <code>match_trait_method</code> to ensure it is the <code>ToOwned</code> method (for example).</p>



<a name="226167453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226167453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226167453">(Feb 12 2021 at 17:20)</a>:</h4>
<p>Oh, TIL <code>ToOwned</code> is a trait, not just a bunch of similarly named inherent methods</p>



<a name="226167654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226167654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226167654">(Feb 12 2021 at 17:21)</a>:</h4>
<p>I was currently planning on using <code>match_def_path(cx, meth_did, &amp;paths::TO_OWNED_METHOD)</code>-- is <code>match_trait_method</code> better here?</p>



<a name="226167746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226167746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226167746">(Feb 12 2021 at 17:22)</a>:</h4>
<p>either is fine</p>



<a name="226169233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226169233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226169233">(Feb 12 2021 at 17:32)</a>:</h4>
<p><span class="user-mention" data-user-id="388092">@Andrea Nall</span> See <code>redundant_clone</code> and <code>str_to_string</code> lints. <code>redundant_clone</code> already detects a bunch of "clone-like" methods and you could re-use that logic. If your lint includes <code>to_string</code>, it could replace the <code>str_to_string</code> lint. But we may want to keep that separate? Not sure.</p>



<a name="226169352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226169352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226169352">(Feb 12 2021 at 17:32)</a>:</h4>
<p>Yeah, I encountered the collision with the <code>str_to_string</code> lint in my initial attempt which was trying to be way too smart.</p>



<a name="226172314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226172314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226172314">(Feb 12 2021 at 17:50)</a>:</h4>
<blockquote>
<p>See the big <code>match</code> with different method names in <code>methods.rs</code>. Instead of having a list, you can just add to that.</p>
</blockquote>
<p>That big match doesn't seem to have the type the method is being called on, wouldn't adding <code>to_vec</code>/<code>to_path_buf</code>/etc there cause false positives, unless I check the method again, as well as the type inside my own lint.</p>
<p>If I just put <code>to_vec</code> in that list, calling <code>ArbitraryType::to_vec</code> would trigger my lint</p>



<a name="226173302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226173302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226173302">(Feb 12 2021 at 17:57)</a>:</h4>
<p>Yes you still need to check the DefId in some way after matching the method name</p>



<a name="226181684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226181684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226181684">(Feb 12 2021 at 19:06)</a>:</h4>
<p>Created draft pr at <a href="https://github.com/rust-lang/rust-clippy/issues/6730">clippy#6730</a>, I think I took all various advice into account</p>



<a name="226225577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226225577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226225577">(Feb 13 2021 at 04:16)</a>:</h4>
<p>by the way, thank you for your help/advice on this</p>



<a name="226267458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226267458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226267458">(Feb 13 2021 at 21:08)</a>:</h4>
<p>Semi-related: I see we're trying to move paths over to diagnostic items (<a href="https://github.com/rust-lang/rust-clippy/issues/5393">clippy#5393</a>).</p>
<p>Especially with lint functions that may be multi-use (such as how we discussed doing this) and will probably have to deal with a mixture of things that already have a diagnostic item and things that do not yet have a diagnostic item.</p>
<p>Do we have an enum type like <code>PathOrDiagnosticItem</code> where that can be passed into the lint instead (containing either a path, or a diagnostic item), and the lint can call <code>path_or_diagnostic_item.matches_type(…)</code> or <code>path_or_diagnostic_item.matches_def_id(…)</code> or the like?  If not would something like that be wanted or useful?</p>



<a name="226267872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226267872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226267872">(Feb 13 2021 at 21:16)</a>:</h4>
<p>I've wondered the same thing. That might be nice. But it's not much more work to just handle both separately.</p>



<a name="226267994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226267994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226267994">(Feb 13 2021 at 21:19)</a>:</h4>
<p>Also that would be thrown away whenever we do fully migrate to  diagnostic items.</p>



<a name="226268109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226268109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226268109">(Feb 13 2021 at 21:22)</a>:</h4>
<p>For how I've got my draft currently, I would possibly require 4 versions of the lint (or a bunch of arguments) to handle all permutations for using diagnostic items over paths. And handling the requested <code>Vec::to_vec</code> or <code>PathBuf::to_path_buf</code> from the ticket seems to require checking both the path of the method and the path of the type</p>



<a name="226268728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226268728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226268728">(Feb 13 2021 at 21:32)</a>:</h4>
<p>You can get the parent of <code>to_vec</code> and check that it is <code>Vec</code>. Then you are only checking one path.</p>



<a name="226268871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226268871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226268871">(Feb 13 2021 at 21:35)</a>:</h4>
<p>would that work even though <code>to_vec</code> is in <code>slice</code>, not <code>Vec</code>?</p>



<a name="226268971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226268971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226268971">(Feb 13 2021 at 21:37)</a>:</h4>
<p>oh right, you'd check for slice then</p>



<a name="226269049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226269049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226269049">(Feb 13 2021 at 21:39)</a>:</h4>
<p>and actually I think you'd need <code>tcx.associated_item</code></p>



<a name="226269237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226269237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226269237">(Feb 13 2021 at 21:42)</a>:</h4>
<p>what I'm doing currently _seems_ to work (just might not be the cleanest way), it just requires taking in both the path to the method and the path to the type of self (and would require 4 variants if I wanted to also support diagnostic items: path+path,path+diagnostic,diagnostic+path,diagnostic+diagnostic)</p>



<a name="226303728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226303728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226303728">(Feb 14 2021 at 13:12)</a>:</h4>
<p>If you are dealing with types or functions from the std library, which I guess you do, you can just add those diagnostic items to rustc and then use them afterwards in Clippy (this also helps with the move to diag items only). I did this in <a href="https://github.com/rust-lang/rust/issues/81277">#81277</a> for example. (If you want to do the clean up in rust-lang/rust-clippy, we have to sync the added diag item to Clippy before you can use them in Clippy though. But I can do that for you.)</p>



<a name="226313991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226313991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226313991">(Feb 14 2021 at 16:51)</a>:</h4>
<p>So basically:</p>
<ul>
<li>make a change in the rust repo adding the diagnostic items to the functions/types I need</li>
<li>have you do the sync and wait for the next nightly</li>
<li>make my changes to my current PR in rust-clippy?</li>
</ul>



<a name="226314056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/226314056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#226314056">(Feb 14 2021 at 16:53)</a>:</h4>
<p>If you don't add any features, but just change paths to diag items, you can also do it directly in the Rust repo. Then you can do all in one PR and don't have to wait for sync/nightly. If you don't mind waiting, yes this would be the workflow</p>



<a name="227636950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227636950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227636950">(Feb 24 2021 at 17:20)</a>:</h4>
<p>it looks like my <code>rust</code> side changes adding the diagnostic items has landed and is now in nightly</p>



<a name="227882855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227882855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227882855">(Feb 26 2021 at 04:58)</a>:</h4>
<p>And now I'm just stuck on checking the diagnostic item on the struct impl the function is from (checking the trait impl works fine if it's on a trait).</p>
<p>If the function is on a trait, the code from <code>match_trait_method</code> was easy enough to hack to work with diagnostic items instead of paths… but…<br>
I can't seem to figure out how to make something like that work for non-trait impl methods.</p>
<p>My best attempt after poking around in the relevant rust guts has my code giving me DefIds like <code>DefId(1:4405 ~ std[e437]::path::{impl#61})</code> for <code>DefId(1:4413 ~ std[e437]::path::{impl#61}::to_path_buf)</code>, (from calling <code>somePathBuf.to_path_buf()</code>) and I can't seem how to figure out how to get to <code>std::path::Path</code> (<code>cx.tcx.def_path_str(def_id)</code> gives me that string, so that is obviously there somewhere but <code>def_path_str</code> is seriously a mess of spaghetti that I've not been able to follow)</p>
<p>And of course the DefId <code>DefId(1:4405 ~ std[e437]::path::{impl#61})</code> lacks my diagnostic item.</p>



<a name="227901187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227901187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227901187">(Feb 26 2021 at 09:15)</a>:</h4>
<p>Can you open a WIP PR of your current state? I can't really help you without seeing code.</p>



<a name="227937670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227937670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227937670">(Feb 26 2021 at 14:47)</a>:</h4>
<p>This is untested code, but could you try making and using a util function like this? Let me know if it makes sense.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">is_diagnostic_assoc_fn</span><span class="p">(</span><span class="n">cx</span><span class="p">,</span><span class="w"> </span><span class="n">fn_def_id</span><span class="p">,</span><span class="w"> </span><span class="n">diag_item_name</span>: <span class="nc">Symbol</span><span class="p">,</span><span class="w"> </span><span class="n">fn_name</span>: <span class="nc">Symbol</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">assoc</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">opt_associated_item</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">assoc</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">fn_name</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">AssocItemContainer</span>::<span class="n">ImplContainer</span><span class="p">(</span><span class="n">impl_id</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">assoc</span><span class="p">.</span><span class="n">container</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">return</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">get_diagnostic_item</span><span class="p">(</span><span class="n">diag_item_name</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">impl_id</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="kc">false</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227946334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227946334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227946334">(Feb 26 2021 at 15:48)</a>:</h4>
<p>I didn't want to commit code that won't pass tests and also would produce a lot of debugging info, wasn't sure how the CI system would like that.</p>
<p>Yeah, that's basically what I came up with (and I have not cleaned up this code yet):</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">match_diagnostic</span><span class="p">(</span><span class="n">cx</span>: <span class="kp">&amp;</span><span class="nc">LateContext</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">expr</span>: <span class="kp">&amp;</span><span class="nc">Expr</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">diag_item</span>: <span class="nc">Symbol</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">def_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">typeck_results</span><span class="p">().</span><span class="n">type_dependent_def_id</span><span class="p">(</span><span class="n">expr</span><span class="p">.</span><span class="n">hir_id</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">associated</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">opt_associated_item</span><span class="p">(</span><span class="n">def_id</span><span class="p">).</span><span class="n">and_then</span><span class="p">(</span><span class="o">|</span><span class="n">associated_item</span><span class="o">|</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">associated_item</span><span class="p">.</span><span class="n">container</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">TraitContainer</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">ImplContainer</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">associated</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"got associated {:?} for {:?} // kind : {:?}"</span><span class="p">,</span><span class="n">assoc_def_id</span><span class="p">,</span><span class="n">def_id</span><span class="p">,</span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_str</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">));</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">" --- {:?} {}"</span><span class="p">,</span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">get_diagnostic_item</span><span class="p">(</span><span class="n">diag_item</span><span class="p">),</span><span class="w"> </span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">get_diagnostic_item</span><span class="p">(</span><span class="n">diag_item</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">assoc_def_id</span><span class="p">));</span><span class="w"></span>

<span class="w">        </span><span class="n">cx</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">is_diagnostic_item</span><span class="p">(</span><span class="n">diag_item</span><span class="p">,</span><span class="w"> </span><span class="n">assoc_def_id</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kc">false</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The ones on struct impls don't work and give me:</p>
<div class="codehilite"><pre><span></span><code>got associated DefId(1:4405 ~ std[e437]::path::{impl#61}) for DefId(1:4413 ~ std[e437]::path::{impl#61}::to_path_buf) // kind : &quot;std::path::Path&quot;
 --- Some(DefId(1:4403 ~ std[e437]::path::Path)) false
</code></pre></div>
<p>While the ones on traits work and get me:</p>
<div class="codehilite"><pre><span></span><code>got associated DefId(5:424 ~ alloc[88c7]::borrow::ToOwned) for DefId(5:426 ~ alloc[88c7]::borrow::ToOwned::to_owned) // kind : &quot;std::borrow::ToOwned&quot;
 --- Some(DefId(5:424 ~ alloc[88c7]::borrow::ToOwned)) true
</code></pre></div>
<p>So I need some way to get from the impl def_id to the struct def_id, hope I didn't stick the diagnostic items in the wrong place</p>



<a name="227947620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227947620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227947620">(Feb 26 2021 at 15:58)</a>:</h4>
<blockquote>
<p>wasn't sure how the CI system would like that.</p>
</blockquote>
<p>Don't worry about that. PR CI nowadays only takes like 7 minutes to run, even less if the build/tests are failing, so that should be fine.</p>



<a name="227950559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227950559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227950559">(Feb 26 2021 at 16:17)</a>:</h4>
<p>ok it looks like you're close. So maybe there is one more step from <code>impl T</code> to <code>T</code> for the non-trait case.</p>



<a name="227951057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227951057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227951057">(Feb 26 2021 at 16:19)</a>:</h4>
<p>and that's the point where I got lost inside the rust guts last night.</p>



<a name="227951150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227951150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227951150">(Feb 26 2021 at 16:19)</a>:</h4>
<p>oooh</p>



<a name="227951289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227951289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227951289">(Feb 26 2021 at 16:20)</a>:</h4>
<p><code>tcx.impl_parent</code>?</p>



<a name="227951397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227951397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227951397">(Feb 26 2021 at 16:20)</a>:</h4>
<p>I actually didn't think the method would cover both the trait and non-trait case so that's awesome if it works!</p>



<a name="227951945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227951945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227951945">(Feb 26 2021 at 16:24)</a>:</h4>
<p>Gives me None if I pass in <code>assoc_def_id</code>, gives me an ICE if I pass in <code>def_id</code></p>



<a name="227952901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227952901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227952901">(Feb 26 2021 at 16:30)</a>:</h4>
<p><code>tcx.type_of(assoc_def_id)</code>?</p>



<a name="227953550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227953550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227953550">(Feb 26 2021 at 16:34)</a>:</h4>
<p>Aha, yes that did it.</p>



<a name="227954603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227954603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Cameron Steffen <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227954603">(Feb 26 2021 at 16:41)</a>:</h4>
<p>Hooray! That part was not very intuitive but I figured it out after looking at usages of <code>tcx.for_each_impl</code> in rustc.</p>



<a name="227960790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20clippy%236715%20%28clippy%236730%29/near/227960790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrea Nall <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20clippy.236715.20(clippy.236730).html#227960790">(Feb 26 2021 at 17:25)</a>:</h4>
<p>And I think that's the PR ready barring any changes needed after review</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>